home *** CD-ROM | disk | FTP | other *** search
- Path: solon.com!not-for-mail
- From: actuary@nando.net (Bill McCarthy)
- Newsgroups: comp.std.c,comp.lang.c.moderated
- Subject: Re: Integral promotion.
- Date: 15 Feb 1996 09:34:19 -0600
- Organization: News & Observer Public Access
- Sender: clc@solutions.solon.com
- Approved: clc@solutions.solon.com
- Message-ID: <4fvjpr$enh@solutions.solon.com>
- References: <4fstj7$2l6@solutions.solon.com>
- Reply-To: actuary@nando.net (Bill McCarthy)
- NNTP-Posting-Host: solutions.solon.com
- X-Newsreader: IBM NewsReader/2 v1.2
-
- In <4fstj7$2l6@solutions.solon.com>,
- Rune Huseby <rune.huseby@gpi.telemax.no> writes:
-
- :I got a problem understanding the rules on integral promotion in
- :the C language:
- :
- :Consider the following function:
- :
- :short test(short x1, short x2);
- :
- :int main(void)
- :{
- : short result;
- : result = test(1, 2);
- : return 0;
- :}
- :
- :short test(short x1, short x2)
- :{
- : short result;
- : result = x1 + x2; /* Warning: '=' : conversion from 'int '
- : to 'short ', possible loss of data */
- : return result;
- :}
- :
- :My compiler (Microsoft Visual C++ 4.0), automagically converts
- :my short-parameters to int's, even though all variables involved
- :are short. I know that the standard says that all arguments can
- :be converted to the biggest 'type' of all the arguments, but is
- :it correct that char's and short's always are promoted to int's,
- :without regard to the other arguments in the expression?
-
- No. Char's and int's will only be promoted to int's if an int can
- represent all values of the original type. Otherwise they will
- be promoted to unsigned int's. This process is called Integral
- Promotion and is described in A6.1 of K&R II. You should read
- the entire section A.6 Conversions (less than three pages) and
- pay special attention to A6.5 Arithmetic Conversions, particu-
- larly if you are switching between difference environments.
-
- The rules of dealing with one operand a long and the other an
- unsigned, differ depending on whether all values of an unsigned
- call be represented by a long. They can be on most 16 bit
- implementations (short=int=16-bit, long=32-bit), but cannot on
- most 32 bit (short=16, int=long=32-bit).
-
- :The reason I ask is that I am writing code that should be
- :portable from a 16-bits environment (where short and int are the
- :same size) to a 32-bits environment. (My 16-bits compiler does
- :not complain about the code).
-
- Since, as you state, short and int are the same size on your 16 bit
- compiler, you can assign from signed int to signed short without
- "possible loss of data."
-
- Bill McCarthy
- actuary@nando.net
- Wendell, NC USA
-